﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Aspose.Words;
using Aspose.Words.Saving;
using System.IO;

namespace DusonFramework.Documents.Office.Word.Aspose
{
    public class Example
    {
        /// <summary>
        /// 生成Word
        /// </summary>
        private void CreateWord()
        {
            // Create an "empty" document. Note that like in Microsoft Word, 
            // the empty document has one section, body and one paragraph in it.
            Document doc = new Document();

            // This truly makes the document empty. No sections (not possible in Microsoft Word).
            doc.RemoveAllChildren();

            // Create a new section node. 
            // Note that the section has not yet been added to the document, 
            // but we have to specify the parent document.
            Section section = new Section(doc);

            // Append the section to the document.
            doc.AppendChild(section);

            // Lets set some properties for the section.
            section.PageSetup.SectionStart = SectionStart.NewPage;
            section.PageSetup.PaperSize = PaperSize.Letter;


            // The section that we created is empty, lets populate it. The section needs at least the Body node.
            Body body = new Body(doc);
            section.AppendChild(body);

            // The body needs to have at least one paragraph.
            // Note that the paragraph has not yet been added to the document, 
            // but we have to specify the parent document.
            // The parent document is needed so the paragraph can correctly work
            // with styles and other document-wide information.
            Paragraph para = new Paragraph(doc);
            body.AppendChild(para);

            // We can set some formatting for the paragraph
            para.ParagraphFormat.StyleName = "Heading 1";
            para.ParagraphFormat.Alignment = ParagraphAlignment.Center;


            // So far we have one empty pararagraph in the document.
            // The document is valid and can be saved, but lets add some text before saving.
            // Create a new run of text and add it to our paragraph.
            Run run = new Run(doc);
            run.Text = "Hello World!";
            run.Font.Color = System.Drawing.Color.Red;
            para.AppendChild(run);

            doc.Save("Word文件保存路径");
        }

        #region 格式转化

        /// <summary>
        /// 将Word保存成网页文件
        /// </summary>
        private void Word2Html_File()
        {
            Document doc = new Document("Word文件路径");
            HtmlSaveOptions saveOptions = new HtmlSaveOptions();
            saveOptions.PrettyFormat = true;
            // This is to allow headings to appear to the left of main text.
            saveOptions.AllowNegativeLeftIndent = true;
            saveOptions.ExportHeadersFooters = false;
            saveOptions.ExportXhtmlTransitional = true;
            saveOptions.SaveFormat = SaveFormat.Html;
            saveOptions.ImagesFolder = "图片保存路径"; 
            saveOptions.ImagesFolderAlias = "图片在文本中显示的路径";

            doc.Save("网页文件保存路径", saveOptions);

            string content = File.ReadAllText("网页文件保存路径");
            content = content.Substring(content.IndexOf("<body>") + 6);
            content = content.Substring(0, content.IndexOf("</body>"));
        }
        /// <summary>
        /// 将Word保存成网页格式的内容（放在内存中）
        /// </summary>
        private void Word2Html_Memory()
        {
            Document doc = new Document("Word文件路径");
            HtmlSaveOptions saveOptions = new HtmlSaveOptions();
            saveOptions.PrettyFormat = true;
            // This is to allow headings to appear to the left of main text.
            saveOptions.AllowNegativeLeftIndent = true;
            saveOptions.ExportHeadersFooters = false;
            saveOptions.ExportXhtmlTransitional = true;
            saveOptions.SaveFormat = SaveFormat.Html;
            saveOptions.ExportImagesAsBase64 = true; // 图片以Base64形式展现

            // 将网页内容保存到内存当中，再读出来
            Stream stream = new MemoryStream();
            doc.Save(stream, saveOptions);
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default);
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            string content = sr.ReadToEnd();

            content = content.Substring(content.IndexOf("<body>") + 6);
            content = content.Substring(0, content.IndexOf("</body>"));
        }

        /// <summary>
        /// 网页内容保存成Word
        /// </summary>
        private void Html2Word()
        {
            Document doc = new Document();
            DocumentBuilder docBuilder = new DocumentBuilder(doc);

            // 将内容中的图片的路径补成绝对路径
            string host = "";
            string html = System.Text.RegularExpressions.Regex.Replace("内容", "src=\"(?:" + host + ")?(.*?)", "src=\"" + host + "/$1");

            docBuilder.InsertHtml(html);

            doc.Save("Word保存路径");
        }
        
        #endregion
    }
}
